package com.zy.pro.system.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zy.core.model.PageView;
import com.zy.core.web.BaseController;
import com.zy.pro.shop.model.Cases;
import com.zy.pro.shop.model.Shop;
import com.zy.pro.shop.service.CasesService;
import com.zy.pro.shop.service.ShopService;
import com.zy.pro.shop.web.form.CasesSearchForm;
import com.zy.pro.shop.web.form.ShopSearchForm;
import com.zy.pro.system.model.Employee;
import com.zy.utils.SessionUtils;

/**
 * 布局控制器
 *
 */
@Controller
@RequestMapping("/control/layoutController")
public class LayoutController extends BaseController {

	private static final Logger logger = Logger.getLogger(LayoutController.class);
	
	/**
	 * 店铺管理服务类
	 */
	@Resource(name = "shopServiceImpl")
	private ShopService shopService;
	
	/**
	 * 案例管理服务类
	 */
	@Resource(name = "casesServiceImpl")
	private CasesService casesService;
	
	/**
	 * 显示引入页面
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/displaySysLogin")
	public String displaySysLogin(HttpServletRequest request, ModelMap map){
		try {
			
			return "control/system/login";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}
	
	
	/**
	 * 显示header
	 * @throws Exception 
	 */
	@RequestMapping("/displayHeader")
	public String displayHeader(HttpServletRequest request,ModelMap map){
		logger.info("displayHeader for LayoutController start...");
		try {
			Employee employee = SessionUtils.getEmpl(request);
			map.addAttribute("employee", employee);
			
			map.addAttribute("ipAddr",this.getIpAddr(request));
			return "control/layout/header";
		} catch (Exception e) {
			logger.error(e);
			return "error";
		}
		
	}
	
	/**
	 * 显示footer
	 */
	@RequestMapping("/displayFooter")
	public String displayFooter(HttpServletRequest request, ModelMap map){
		map.addAttribute("loginTime", request.getSession().getAttribute("loginTime"));
		return "control/layout/footer";
	}
	
	/**
	 * 显示引入页面
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping("/displayIndex")
	public String displayIndex(ShopSearchForm shopForm,CasesSearchForm casesForm,HttpServletRequest request, ModelMap map){
		//map.addAttribute("loginTime", request.getSession().getAttribute("loginTime"));
		try {
			//查询最新店铺信息
			shopForm.getOrderby().put("shopCreateTime", "desc");
			shopForm.setShopStatus(1);//正常使用的店铺(1:正常,2:冻结,3:删除)
			PageView<Shop> shopPageView = new PageView<Shop>(10, shopForm.getPage());
			shopForm.setFirstResult(shopPageView.getFirstResult());
			shopForm.setMaxResult(shopPageView.getMaxresult());
			shopPageView.setQueryResult(shopService.getData(shopForm));
			map.addAttribute("shopPageView", shopPageView);
			map.addAttribute("shopForm", shopForm);
			
			//查询最新案例信息
			casesForm.getOrderby().put("caseCreateTime", "desc");
			//设置城市主键
			if(shopForm.getCityId() != null){
				casesForm.setCaseCityId(shopForm.getCityId());
			}
			
			PageView<Cases> casesPageView = new PageView<Cases>(10, casesForm.getPage());
			casesForm.setFirstResult(casesPageView.getFirstResult());
			casesForm.setMaxResult(casesPageView.getMaxresult());
			casesPageView.setQueryResult(casesService.getData(casesForm));
			map.addAttribute("casesPageView", casesPageView);
			map.addAttribute("casesForm", casesForm);

			
			return "control/index";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}
	
	
	/**
	 * 退出
	 * @param request
	 * @return
	 */
	@RequestMapping("/loginOut")
	public String loginOut(String roleType,Integer cityId,ModelMap map,HttpServletRequest request){
		if("1".equals(roleType)){
			//删除session中的用户信息
			SessionUtils.removeUser(request);
		}else if("2".equals(roleType)){
			//删除session中的店家信息
			SessionUtils.removeShopUser(request);
		}else if("3".equals(roleType)){
			//删除session中的管理员信息
			SessionUtils.removeEmpl(request);
		}
		//删除角色类型
		SessionUtils.removeRoleType(request);
		
		ShopSearchForm shopSearchForm = new ShopSearchForm();
		if(cityId != null){
			shopSearchForm.setCityId(cityId);
		}
		
		if("3".equals(roleType)){
			//删除session中的管理员信息
			return "/control/system/login";
		}
		return displayIndex(shopSearchForm,new CasesSearchForm(),request,map);
	}
}
